<?php
declare (strict_types=1);

namespace app\advertiser\validates;

use think\Validate;

/**
 * 广告主AdvertiserUserValidate验证器
 */
class AdvertiserUserValidate extends Validate
{
    /**
     * 验证规则
     * @var array
     */
    protected $rule = [
        'id' => 'require|number',
        'name' => 'chsDash|max:50',
        'code' => 'require|number',
        'phone' => 'require|checkMobile',
        'password' => 'require|max:32|alphaNum',
    ];

    /**
     * 字段描述
     * @var string[]
     */
    protected $field = [
        'id' => 'ID',
        'name' => '名称',
        'code' => '验证码',
        'phone' => '手机号',
        'password' => '密码',
    ];

    /**
     * 验证场景
     * @var array
     */
    protected $scene = [
        'register' => ['name', 'phone', 'password', 'code'],
        'getCode' => ['phone'],
        'login' => ['phone', 'password'],
        'adUserPwd' => ['password',],
    ];

    /**
     * 验证手机号格式
     * @param string $value
     * @param string $rule
     * @param array $data
     * @return bool|string
     */
    protected function checkMobile(string $value, string $rule, array $data)
    {
        $preg = '/^1[3-9]\d{9}$/';
        if (!preg_match($preg, $value)) {
            return '手机号码格式错误';
        }
        return true;
    }


}